<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        h2,
        p {
            margin: 0;
        }

        .box {
            margin: 30px auto;
            width: 300px;
        }

        #user,
        #message {
            box-sizing: border-box;
            width: 300px;
            border: 1px solid #000;
            padding: 10px;
            height: 100px;
            resize: none;
            display: block;
        }

        #user {
            padding: 0 10px;
            height: 30px;
            margin-bottom: 10px;
        }

        #btn {
            display: block;
            margin: 10px auto;
            width: 60%;
            height: 40px;
        }

        #list {
            border-top: 1px solid #000;
            padding: 10px 0 0;
            margin: 0;
            list-style: none;
        }

        #list li {
            position: relative;
            font: 14px/30px "宋体";
            border-bottom: 1px solid #000;
            padding: 0 15px;
            overflow: hidden;
        }

        .clos {
            position: absolute;
            right: 5px;
            top: 10px;
        }

        .clos2 {
            position: absolute;
            right: 5px;
            top: 30px;
        }

        .replay {
            position: absolute;
            right: 5px;
            top: 35px;
        }

        .mask {
            width: 100vw;
            height: 100vh;
            background: black;
            opacity: .3;
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }

        .comfirm {
            width: 200px;
            height: 120px;
            background: white;
            text-align: center;
            position: absolute;
            left: 50%;
            top: 30%;
            border-radius: 5px;
            transform: translate(-50%, -50%);
            overflow: hidden;
            display: none;

        }

        .comfirm button {
            /* position: absolute; */
            /* right: 10px; */
            float: right;
            margin-top: 20px;
            margin-right: 10px;
        }
    </style>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>

</head>

<body>
    <div class="box">
        <input type="text" id="user" value="小明" />
        <textarea id="message">我就是传说中的无敌小明</textarea>
        <button id="btn">提交留言</button>
        <ul id="list">
            <li style="height: auto">
                <h2 class="user">小王说:</h2>
                <p>呼叫无敌小明</p>

                <a href="javascript:;" class="clos">删除</a>
                <a href="javascript:;" class="clos2">回复</a>
            </li>
            <li style="height: auto">
                <h2 class="user">老师说:</h2>
                <p>小明滚出去</p>
                <p>回复的内容</p>
                <a href="javascript:;" class="clos">删除</a>
            </li>
        </ul>


    </div>
    <script>
        // 先把ul清空掉 
        $("#list").html("");
        $("#btn").click(function () {
            var username = $("#user").val();
            var message = $("#message").val();
            var liEle = $("<li></li>", {
                style: "height: auto"
            }).html(`<h2 class="user">${username}说:</h2>
            <p>${message}</p>
            <a href="javascript:;" class="clos">删除</a>
            <a href="javascript:;" class="clos2">回复</a>
            <div class="replayContainer" style="display:none;">
                    <input type="text" /> <button class="sure">确定</button>
            </div>
            `);
            $("#list").append(liEle);

            // 删除操作 
            $(".clos").click(function(){
                $(this).parent().remove();
            });

            // 给回复按钮绑定事件
            // 绑定事件之前 把之前的点击事件解除绑定
            $(".clos2").off("click");


            // jq里会重复绑定事件 
            $(".clos2").on("click",function(){
                // console.log("点击了回复按钮")
                $(this).next().css("display","block");
            })

            // 获取确定按钮
            $(".sure").off("click");
            $(".sure").on("click",function(){
                // console.log("点击了");
                var val = $(this).prev().val();  // 获取input框里的值
                var pEle = $("<p></p>").html(val);
                $(this).parent().parent().append(pEle);
                 // 把回复的框隐藏起来
                 $(this).parent().css("display","none");
            })


        })
        

    </script>
</body>

</html>